/*
 * @Author: Ryan Huang
 * @Date: 2019-04-07 20:57:25
 * @Description:format QQ number to SQL
 */
package main

import (
	"bufio"
	"database/sql"
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"

	_ "github.com/go-sql-driver/mysql"
)

const (
	Path   = "/home/hwanginsitein/Videos/Golang/兄弟连GO语言提升/day22"
	driver = "mysql"
	dns    = "root:root@tcp(127.0.0.1:3306)/users?charset=ASCII"
)

func main() {
	f, _ := os.Open(Path + "/QQ.txt")
	defer f.Close()
	buf := bufio.NewReader(f)
	db, _ := sql.Open(driver, dns)
	var i int
	//var sqlState = "insert into qq values(?,?)"
	sqlState := "insert into qq (qq,password) values "
	var sqlValues []interface{}
	for {
		i++
		line, _, _ := buf.ReadLine()
		qq_pass := strings.Split(string(line), "----")
		qq, _ := strconv.Atoi(qq_pass[0])
		//ufffd
		password := qq_pass[1]
		/*
			if qq == 418432969 || qq == 1099028271 || qq == 1051964276 || qq == 402839652 || qq == 910441016 || qq == 121693758 || qq == 498744857 ||
				qq == 514864753 || qq == 360742017 || qq == 237719290 || qq == 173594905 || qq == 296144155 || qq == 408308954 || qq == 122438211 ||
				qq == 369332281 || qq == 624476750 || qq == 406892890 || qq == 472155124 || qq == 409012541 || qq == 315305392 || qq == 383742405 ||
				qq == 919764370 || qq == 601338057 || qq == 295546987 || qq == 421630912 || qq == 312638625 || qq == 645887758 || qq == 847686660 ||
				qq == 619412420 || qq == 569894808 {
				value := make([]interface{}, 0)
				value = append(value, qq)
				value = append(value, password)
				jsonValues, _ := json.Marshal(value)

				if n := strings.Index(string(jsonValues), "\\ufff"); n != -1 {
					fmt.Println("right")
					continue
				}
				os.Exit(0)
				continue
			}
		*/
		value := make([]interface{}, 0)
		value = append(value, qq)
		value = append(value, password)
		jsonValues, _ := json.Marshal(value)

		if n := strings.Index(string(jsonValues), "\\ufff"); n != -1 {
			continue
		}
		sqlValues = append(sqlValues, qq)
		sqlValues = append(sqlValues, password)
		//sqlState = sqlState + "(" + qq_pass[0] + ",'" + password + "'),"
		sqlState = sqlState + "(?,?),"
		if i%3000 == 0 {
			sqlState = sqlState[:len(sqlState)-1]
			stmt, err := db.Prepare(sqlState)
			res, err := stmt.Exec(sqlValues...)

			if err != nil {
				fmt.Println(11111)
				fmt.Println(err)
				fmt.Println(res)
				jsonValues, _ := json.Marshal(sqlValues)
				fmt.Println(string(jsonValues))
				os.Exit(0)
			}
			sqlState = "insert into qq (qq,password) values "
			sqlValues = make([]interface{}, 0)
		}
	}
	sqlState = sqlState[:len(sqlState)-1]
	stmt, _ := db.Prepare(sqlState)
	_, err1 := stmt.Exec(sqlValues...)
	if err1 != nil {
		fmt.Println(22222)
		fmt.Println(sqlValues)
		fmt.Println(err1)
	}
}
